package android.test.devicetest;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemProperties;
import android.test.getintent.GetIntent;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
import android.test.until.Max3HWVerion;

public class KeyCode extends Activity {

    private TextView pad_up;
    private TextView pad_down;
    private TextView pad_back;
    private TextView pad_ok;
    private TextView serial_num;
    private TextView gprs_status;
    private TextView map_status;
    private TextView cur_time;
    private int numback = 0;
    private int numenter= 0;

    Handler  handler  = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            long  epoch = System.currentTimeMillis() / 1000;
            String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date(epoch * 1000));
            cur_time.setText("Current Time\t\t: " + date);
            handler.postDelayed(runnable, 1000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keycode);
        pad_back    = (TextView) findViewById(R.id.pad_back);
        pad_down    = (TextView) findViewById(R.id.pad_down);
        pad_up      = (TextView) findViewById(R.id.pad_up);
        pad_ok      = (TextView) findViewById(R.id.pad_center);
        serial_num  = (TextView) findViewById(R.id.serial_num);
        gprs_status = (TextView) findViewById(R.id.gprs_status);
        map_status  = (TextView) findViewById(R.id.map_status);
        cur_time    = (TextView) findViewById(R.id.cur_time);

        String gprs = Max3HWVerion.isGPRSHardwareOK() ? "OK" : "NG";
        String map  = Max3HWVerion.isMapApkDataOK  () ? "OK" : "NG";
        String str  = String.format("Serial Number\t: %s\nROM Version\t\t: %s\nHW Version\t\t: %s\nGPRS Version\t: %s\n%s",
                        Max3HWVerion.getSerialNumber(),
                        SystemProperties.get("ro.max3.rom.version", "unknow"),
                        Max3HWVerion.getHWVersion(),
                        Max3HWVerion.getGPRSVersion(),
                        Max3HWVerion.getGPSVersion());
        serial_num.setText(str);

        str = String.format("GPRS HW\t\t\t\t: %s", gprs);
        gprs_status.setText(str);
        if (gprs.equals("NG")) {
            gprs_status.setTextColor(Color.rgb(255, 64, 64));
        }

        str = String.format("Map Data\t\t\t\t: %s", map);
        map_status.setText(str);
        if (map.equals("NG")) {
            map_status.setTextColor(Color.rgb(255, 64, 64));
        }

        // for time displaying
        handler.postDelayed(runnable, 1000);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        handler.removeCallbacks(runnable);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            pad_down.setBackgroundColor(Color.YELLOW);
            break;

        case KeyEvent.KEYCODE_DPAD_RIGHT:
            pad_up.setBackgroundColor(Color.YELLOW);
            break;

        case KeyEvent.KEYCODE_BACK:
            pad_back.setBackgroundColor(Color.YELLOW);
            return true;

        case KeyEvent.KEYCODE_ENTER:
            pad_ok.setBackgroundColor(Color.YELLOW);
            break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode != KeyEvent.KEYCODE_BACK) {
            numback = 0;
        }

        if (keyCode != KeyEvent.KEYCODE_ENTER) {
            numenter= 0;
        }

        switch (keyCode) {

        case KeyEvent.KEYCODE_DPAD_LEFT:
            pad_down.setBackgroundColor(Color.GREEN);
            break;

        case KeyEvent.KEYCODE_DPAD_RIGHT:
            pad_up.setBackgroundColor(Color.GREEN);
            break;

        case KeyEvent.KEYCODE_BACK:
            pad_back.setBackgroundColor(Color.GREEN);
            if (++numback == 2) {
                finish();
                System.exit(0);
            }
            return true;

        case KeyEvent.KEYCODE_ENTER:
            pad_ok.setBackgroundColor(Color.GREEN);
            if (++numenter== 2) {
                finish();
                Intent mIntent = GetIntent.getItemIntent(KeyCode.this, "rgb");
                if (mIntent != null) {
                    startActivity(mIntent);
                }
            }
            break;
        }
        return super.onKeyUp(keyCode, event);
    }

}

